#include "stdafx.h"

#include "FASTApplication.h"

#include <Codecs/XMLTemplateParser.h>
#include <Codecs/TemplateRegistry.h>

#include <iosfwd>

namespace
{
  // On Windows we need to open a FAST data file in binary mode to avoid translation of end of line characters.
  // Other platforms do not have this requirement so we make openMode a "nop" for them.
#ifdef _WIN32
  const std::ios::openmode openMode = std::ios::binary | std::ios::in;
#else
  const std::ios::openmode openMode = std::ios::in;
#endif
}

using namespace QuickFAST;

///////////////////////
// Basic FAST Application

FASTApplication::FASTApplication()
{
}

FASTApplication::~FASTApplication()
{
}

bool
FASTApplication::init(int argc, char * argv[])
{
  std::string templateFileName;

  bool ok = false;
  if(argc == 5)
  {
    templateFileName = argv[1];
    ip_ = argv[2];
    port_ = argv[3];
    mode_ = argv[4];

	try
	{
		////////////////////////////////////////////////
		// Open files first to be sure they are present.
		std::ifstream templates(templateFileName.c_str(), openMode);
		if(!templates.good())
		{
			std::cerr << "ERROR: Can't open template file: " << templateFileName << std::endl;
		}
		else
		{
			/////////////////////////////////////////////
			// Parse the templates from the template file
			// errors are reported by throwing exceptions
			// which are caught below.
			Codecs::XMLTemplateParser parser;
			pRegistry_ = parser.parse(templates);
			ok = true;
		}
	}
	catch (std::exception & e)
	{
	std::cerr << "FASTApplication::init: Exception:" << e.what() << std::endl;
	}
  }
  else
  {
	  std::cout << "Usage: appName [Template File Name] [ip] [port] [mode: 1,2,3]" << std::endl;
  }
  return ok;
}

void
FASTApplication::fini()
{
}
